<script>
import { configHouse } from '@/config/house/houseDetailConfig';
import { mapState } from 'vuex';
import SingleInput from './com/SingleInput';
import MultipleInput from './com/MultipleInput';
import SingleSelect from './com/SingleSelect';
import CheckBox from './com/CheckBox';
import { updateDelInfoWeb } from '@/api/user';
import { SALES } from '@/config/house/houDelVOKeys';

const titles1 = ['建筑面积', '售价', '户型'];
const titles2 = [
  '建筑面积',
  '售价',
  '户型',
  '房屋类型',
  '产权类型',
  '朝向',
  '装修',
  '产权期限',
  '房屋性质',
  '建筑形式',
  '是否配有电梯',
  '佣金比例',
  '建造年代',
  '房源年限',
  '唯一住房',
  '总楼层',
  '所在楼层',
  '建筑面积',
  '使用面积',
  '配套设施',
];

export default {
  name: 'ComBaseInfoMiss',
  shulan: true,
  props: {
    type: String,
  },
  data() {
    return {
      modifiedCount: 1,
    };
  },
  render() {
    const { houDelVO, type } = this;
    const titles = type == 'edit' ? titles2 : titles1;
    const config = configHouse.filter((v) => titles.indexOf(v.title) > -1);

    const doms = config.map((configItem, index) => {
      return (
        <el-form-item
          label={configItem.title}
          key={index}
          prop={configItem.key}
          rules={configItem.rules || []}
        >
          {this.createNodes(houDelVO, configItem, type)}
        </el-form-item>
      );
    });
    return (
      <el-form class="wrap" props={{ model: houDelVO, 'label-width': '110px', size: 'mini', inline: true }} style="margin-top: 20px">
        {doms}
      </el-form>
    );
  },
  created() {},
  mounted() {
    if (!this.houDelVO.portHouNum) {
      this.houDelVO.portHouNum = '1' + String(Math.random()).slice(-9);
    }
  },
  watch: {
    houDelVO: {
      deep: true,
      handler() {
        this.modifiedCount--;
      },
    },
  },
  computed: {
    ...mapState({
      houDelVO: (state) => state.houseInfo.houDelVO,
    }),
  },
  methods: {
    createNodes(houDelVO, configItem, type) {
      const props = { houDelVO, configItem, type };
      if (configItem.isMultiple) {
        return <MultipleInput props={props} />;
      } else if (configItem.isInput) {
        return <SingleInput props={props} />;
      } else if (configItem.isRadio) {
        return <SingleSelect props={props} />;
      } else if (configItem.isCheckbox) {
        return <CheckBox props={props} />;
      }
    },

    // 保存房源基本信息
    save() {
      if (this.modifiedCount > 0) return Promise.resolve();

      const { houDelVO } = this;
      let vo = {};
      for (const key in SALES) {
        if (key in houDelVO) {
          vo[key] = houDelVO[key];
        }
      }

      vo = {
        ...vo,
        totalPrices: this.setTotalPrices(vo),
        matchingFacilities: vo.matchingFacilitiesList.join(','),
      };

      return updateDelInfoWeb({
        houDelStr: JSON.stringify(vo),
      }).then(({ isSuccess, errorMessage }) => {
        if (!isSuccess) {
          return Promise.reject({ errorMessage: errorMessage || '房源基本信息保存失败！' });
        }
      });
    },
  },
};
</script>
<style scoped>
.wrap >>> .el-row .el-form-item--mini {
  margin-bottom: 0px;
}
</style>
